package com.songcan.common.vo;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Set;

@Data
public class UserDetailsNew implements UserDetails {
    private static final long serialVersionUID = 500L;
    private Long userId;
    private String name;
    private String password;  //密码
    private final String username; //用户名
    private final String salt;  //加密key
    private final Collection<GrantedAuthority> authorities; //角色集合
    private final boolean accountNonExpired;  //有限时长
    private final boolean accountNonLocked;  //用户是否锁定
    private final boolean credentialsNonExpired;  //是否激活
    private final boolean enabled;  //是否启用
    private Set<String> organizations;




    public UserDetailsNew(String username, String salt, String password, Collection<GrantedAuthority> authorities, boolean enabled, Long userId, String name) {
        this(username, salt,password, enabled, true, enabled, true, authorities,userId,name);
    }
    public UserDetailsNew(String username, String salt, String password, Collection<GrantedAuthority> authorities, boolean enabled, Long userId, Boolean isLock, String name) {
        this(username, salt,password, enabled, true, true, isLock, authorities,userId,name);
    }
    public UserDetailsNew(String username,  Collection<GrantedAuthority> authorities, boolean enabled, Long userId, Boolean isLock, String name) {
        this(username,  enabled, true, enabled, isLock, authorities,userId,name);
    }

    private UserDetailsNew(String username, String salt, String password, boolean enabled, boolean accountNonExpired,
                           boolean credentialsNonExpired, boolean accountNonLocked, Collection<GrantedAuthority> authorities, Long userId, String name) {
        if (username != null && !"".equals(username) && password != null) {
            this.username = username;
            this.salt = salt;
            this.password = password;
            this.enabled = enabled;
            this.accountNonExpired = accountNonExpired;
            this.credentialsNonExpired = credentialsNonExpired;
            this.accountNonLocked = accountNonLocked;
            this.authorities = authorities;
            this.userId = userId;
            this.name = name;
        } else {
            throw new IllegalArgumentException("Cannot pass null or empty values to constructor");
        }
    }

    public UserDetailsNew(String username,  boolean enabled, boolean accountNonExpired,
                          boolean credentialsNonExpired, boolean accountNonLocked, Collection<GrantedAuthority> authorities, Long userId, String name){
        //三方登录需要
        if (username != null && !"".equals(username)) {
            this.username = username;
            this.enabled = enabled;
            this.accountNonExpired = accountNonExpired;
            this.credentialsNonExpired = credentialsNonExpired;
            this.accountNonLocked = accountNonLocked;
            this.authorities = authorities;
            this.userId = userId;
            this.name = name;
            this.salt = "";
            this.password = "";
        }else {
            throw new IllegalArgumentException("Cannot pass null or empty values to constructor");
        }
    }

    public void  setOrganizations(Set<String> param){
        this.organizations = param;
    }
}
